﻿#region

using System;
using System.Xml;

#endregion

namespace ZhhbSoft.Data.Criterion
{
    /// <summary>
    ///Data 的摘要说明
    /// </summary>
    public class Expression : Node
    {
        private ExpressionCollection _Expressions = new ExpressionCollection();
        private ItemCollection _Items = new ItemCollection();

        public ExpressionCollection Expressions
        {
            get { return _Expressions; }
            set { _Expressions = value; }
        }
        public ItemCollection Items
        {
            get { return _Items; }
            set { _Items = value; }
        }
        public string Sgn
        {
            get { return Attributes["sgn"]; }
            set { Attributes["sgn"] = value; }
        }
        public string Name
        {
            get { return Attributes["name"]; }
            set { Attributes["name"] = value; }
        }

        public Expression()
            : this("and")
        {
        }
        public Expression(string sgn)
            : this(sgn, Guid.NewGuid().ToString())
        {
        }
        public Expression(string sgn, string name)
            : base("Expression")
        {
            Attributes["sgn"] = sgn;
            Attributes["name"] = name;
            _childs = new NodeCollection();
            ((NodeCollection)_childs).Expression = this;
            _Expressions.Expression = this;
            _Items.Expression = this;
        }
        public override void FillFromXML(XmlNode xmlExpression)
        {
            try
            {
                Attributes["name"] = xmlExpression.Attributes["name"].Value;
                Attributes["sgn"] = xmlExpression.Attributes["sgn"].Value;
            }
            catch (Exception)
            {

            }
            foreach (XmlNode xmlNode in xmlExpression.SelectSingleNode("item|Expression"))
            {
                Node node;
                if (xmlNode.Name == "item")
                {
                    node = new Item(xmlNode.Attributes["columnName"].Value, xmlNode.Attributes["operator"].Value, xmlNode.InnerText, xmlNode.Attributes["dataType"].Value, xmlNode.Attributes["joinType"].Value);
                    _Items.Add(node);
                }
                else
                {
                    node = new Expression(xmlNode.Attributes["sgn"].Value, xmlNode.Attributes["name"].Value);

                    node.FillFromXML(xmlNode.SelectSingleNode("."));
                    _Expressions.Add(node);
                }
                _childs.Add(node);
            }
        }
        public override string ToString()
        {
            string strTemp = "";
            if (_childs.Count > 0)
            {
                strTemp += "(";
                foreach (Node node in _childs)
                {
                    switch (Attributes["sgn"])
                    {
                        default:
                            {
                                strTemp += node + " " + Attributes["sgn"] + " ";
                                break;
                            }
                    }
                }
                strTemp = strTemp.Remove(strTemp.LastIndexOf(Attributes["sgn"] + " "));
                strTemp += ")";
            }
            return strTemp;
        }
    }
}